package com.mall.mapper;

import com.mall.domain.po.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户Mapper
 * @author 勾新杰
 * @version 1.0
 * @date 2024/11/22 0:02
 */
@Mapper
public interface UserMapper {

    /**
     * 新增用户
     * @param user 用户
     */
    @Insert("insert into mall.user(username, password, email, phone, create_time, last_login, status, balance) " +
            "VALUES (#{username}, #{password}, #{email}, #{phone}, #{createTime}, #{lastLogin}, #{status}, #{balance})")
    void insert(User user);

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户
     */
    @Select("select id, username, password, email, phone, create_time, last_login, status, balance from user where username = #{username}")
    User getUserByUsername(String username);

    /**
     * 根据用户id查询用户
     * @param userId 用户id
     * @return 用户
     */
    @Select("select id, username, password, email, phone, create_time, last_login, status, balance from user where id = #{userId};")
    User getUserById(Long userId);

    /**
     * 修改密码
     * @param userId 用户id
     * @param newPassword 新密码
     */
    @Update("update user set password = #{newPassword} where id = #{userId}")
    void updatePassword(Long userId, String newPassword);

    /**
     * 更新用户信息
     * @param user 用户
     */
    void update(User user);

    /**
     * 充值
     * @param amount 金额
     * @param id 用户id
     */
    @Update("update user set balance = balance + #{amount} where id = #{id}")
    void inductBalance(Double amount, Long id);

    /**
     * 扣款
     * @param amount 金额
     * @param id 用户id
     */
    @Update("update user set balance = balance - #{amount} where id = #{id}")
    void deductBalance(Double amount, Long id);
}
